package com.ybkj.daijia.api.weixin.handler.impl;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.WxSetting;
import com.ybkj.daijia.api.weixin.WxMsgType;
import com.ybkj.daijia.api.weixin.entity.WxBaseMsgEntity;
import com.ybkj.daijia.api.weixin.entity.WxBaseRespEntity;
import com.ybkj.daijia.api.weixin.entity.WxItemPicDescEntity;
import com.ybkj.daijia.api.weixin.entity.WxMsgEventEntity;
import com.ybkj.daijia.api.weixin.entity.WxRespPicDescEntity;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxConsumersManagerEventHandler extends AbstractWxEventHandler {

    @Autowired
    private SettingUtils settingUtils;


    @Override
    public boolean canHandle(WxBaseMsgEntity msg) {
        if (super.canHandle(msg)) {
            if ("ConsumerManager".equals(((WxMsgEventEntity) msg).getEventKey())) {
                return true;
            }
        }
        return false;
    }


    @Override
    public WxBaseRespEntity handleEvent(WxMsgEventEntity msg,
        Map<String, Object> context, WxBaseRespEntity resp) {

        WxSetting setting = settingUtils.getWx();

        //客户经理
        WxRespPicDescEntity news = new WxRespPicDescEntity();

        List<WxItemPicDescEntity> articles = new LinkedList<WxItemPicDescEntity>();

        WxItemPicDescEntity item = new WxItemPicDescEntity();
        item.setTitle("客户经理");
        String pic = String.format("%sassets/img/ConsumerManager.png", setting.getWxServer());
        String url = String
            .format("%s/wx/v1/ConsumerManager/profile?wxuser=%s", setting.getWxServer(),
                msg.getFromUserName());
        item.setPicUrl(pic);
        item.setUrl(url);
        articles.add(item);

        //马上下单
        item = new WxItemPicDescEntity();
        pic = String.format("%sassets/img/makemoney.png", setting.getWxServer());
        url = String.format("%swx/v1/ConsumerManager/book?wxuser=%s", setting.getWxServer(),
            msg.getFromUserName());
        item.setTitle("我是客户经理我要帮客户下单");
        item.setPicUrl(pic);
        item.setUrl(url);
        articles.add(item);

        //成为客户经理
        item = new WxItemPicDescEntity();
        pic = String.format("%sassets/img/apply.png", setting.getWxServer());
        url = String.format("%swx/v1/ConsumerManager/apply?wxuser=%s", setting.getWxServer(),
            msg.getFromUserName());
        item.setTitle("还不是客户经理？马上成为客户经理");
        item.setPicUrl(pic);
        item.setUrl(url);
        articles.add(item);

        //已经是客户经理，绑定
        item = new WxItemPicDescEntity();
        pic = String.format("%sassets/img/bind.png", setting.getWxServer());
        url = String.format("%swx/v1/ConsumerManager/bind?wxuser=%s", setting.getWxServer(),
            msg.getFromUserName());
        item.setTitle("我已经是客户经理，立即绑定");
        item.setPicUrl(pic);
        item.setUrl(url);
        articles.add(item);

        news.setArticles(articles);
        news.setCreatedDate(new Date());
        news.setCreateTime(news.getCreatedDate().getTime());
        news.setFromUserName(msg.getToUserName());
        news.setToUserName(msg.getFromUserName());
        news.setFuncFlag(0);
        news.setMsgType(WxMsgType.news.name());

        return news;

    }

}
